package com.littlec.sdk.utils;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.os.Environment;
import android.util.Base64;

public class CommonUtils {
	public static boolean isExitsSdcard() {
		if (Environment.getExternalStorageState().equals("mounted")) {
			return true;
		}
		return false;
	}

	public static String md5Digest32(String input) {
		String str = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(input.getBytes());
			byte[] b = md.digest();

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				int i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return str;
	}

	public static String base64Encode(byte[] input) {
		return Base64.encodeToString(input, 0);
	}

	public static byte[] base64Decode(String input) {
		return Base64.decode(input, 0);
	}

	public static String getDataDir(Context context) {
		String state = Environment.getExternalStorageState();
		String dir = null;
		if ("mounted".equals(state))
			dir = Environment.getExternalStorageDirectory().getAbsolutePath();
		else {
			dir = context.getFilesDir().getAbsolutePath();
		}
		return dir;
	}

	public static void delete(File file) {
		if (file.isFile()) {
			file.delete();
			return;
		}

		if (file.isDirectory()) {
			File[] childFiles = file.listFiles();
			if ((childFiles == null) || (childFiles.length == 0)) {
				file.delete();
				return;
			}

			for (int i = 0; i < childFiles.length; i++) {
				delete(childFiles[i]);
			}
			file.delete();
		}
	}
}
